﻿Imports IWshRuntimeLibrary
Imports System.Runtime.InteropServices
Imports Wys.Control.Vb.AdjustFormModule
Public Class MyFileEdit
    Inherits System.Windows.Forms.TextBox
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        '在此处添加自定义绘制代码
    End Sub
    ''' <summary>
    ''' 拖拽 不能显示快捷方式的路径
    ''' </summary>
    ''' <param name="drgevent"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnDragDrop(ByVal drgevent As System.Windows.Forms.DragEventArgs)
        MyBase.OnDragDrop(drgevent)
        Me.Text = CType(drgevent.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()
    End Sub
    ''' <summary>
    ''' 拖拽控件进入
    ''' </summary>
    ''' <param name="drgevent"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnDragEnter(ByVal drgevent As System.Windows.Forms.DragEventArgs)
        MyBase.OnDragEnter(drgevent)
        If (drgevent.Data.GetDataPresent(DataFormats.FileDrop)) Then
            drgevent.Effect = DragDropEffects.Link
        Else
            drgevent.Effect = DragDropEffects.None
        End If
    End Sub

    ''' <summary>
    ''' 添加拖拽属性
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Property AllowDrop() As Boolean
        Get
            Return True
        End Get
        Set(ByVal value As Boolean)
            MyBase.AllowDrop = True
        End Set
    End Property
    ''' <summary>
    ''' 设置能保存操作大于2M的文件
    ''' </summary>
    ''' <remarks></remarks>
    Private _BSaveBlobData As Boolean = False
    Public Property BSaveBlobData() As Boolean
        Get
            Return _BSaveBlobData
        End Get
        Set(ByVal value As Boolean)
            _BSaveBlobData = value
        End Set
    End Property

    ''' <summary>
    ''' 判断文件的大小，如果大于10M的话，报错，清楚内容
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)

            If (System.IO.File.Exists(value)) Then
                '为快捷方式 本地文件
                Dim bLink As Boolean = False
                If (value.EndsWith(".lnk")) Then
                    Dim shell As IWshShell_Class = New IWshShell_ClassClass()
                    Dim shortCut As IWshRuntimeLibrary.IWshShortcut = CType(shell.CreateShortcut(value), IWshShortcut)
                    If (shortCut IsNot Nothing) Then
                        MyBase.Text = shortCut.TargetPath
                        bLink = True
                    Else
                        Return
                    End If
                End If
                '网址
                If (value.EndsWith(".url")) Then
                    Dim shell As IWshShell_Class = New IWshShell_ClassClass()
                    Dim shortCut As IWshRuntimeLibrary.IWshURLShortcut = CType(shell.CreateShortcut(value), IWshURLShortcut)
                    If (shortCut IsNot Nothing) Then
                        MyBase.Text = ""
                        ShowMessageBoxModule("此为网页链接,本软件不支持，链接目标地址为：" + shortCut.TargetPath)
                        Return
                    End If
                End If
                '为文件夹
                If (System.IO.Directory.Exists(value)) Then
                    MyBase.Text = ""
                    ShowMessageBoxModule("不要拖入文件夹")
                    Return
                End If
                '为Link的时候
                If (bLink) Then
                    If Not (BSaveBlobData) Then
                        If (My.Computer.FileSystem.GetFileInfo(Me.Text).Length > 1024 * 1024 * 2) Then
                            ShowMessageBoxModule("文件超过2M")
                            MyBase.Text = ""
                            Return
                        Else
                            Return
                        End If
                    Else
                        MyBase.Text = value
                    End If
                Else
                    If Not (BSaveBlobData) Then
                        If (My.Computer.FileSystem.GetFileInfo(value).Length > 1024 * 1024 * 10) Then
                            ShowMessageBoxModule("文件超过2M")
                            MyBase.Text = ""
                            Return
                        Else

                            MyBase.Text = value
                        End If
                    Else
                        MyBase.Text = value
                    End If

                End If

            Else
                MyBase.Text = value
            End If
        End Set
    End Property
End Class
